package com.edu.exam.entity.core;

import com.edu.exam.enums.core.EntityStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 题型分类实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：question_types
 * 管理考试题目的类型分类
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @NotBlank(message = "题型代码不能为空")
    @Size(max = 20, message = "题型代码长度不能超过20个字符")
    @Column(name = "type_code", nullable = false, length = 20, unique = true)
    private String typeCode;

    @NotBlank(message = "题型名称不能为空")
    @Size(max = 50, message = "题型名称长度不能超过50个字符")
    @Column(name = "type_name", nullable = false, length = 50)
    private String typeName;

    @Size(max = 100, message = "题型英文名长度不能超过100个字符")
    @Column(name = "type_name_en", length = 100)
    private String typeNameEn;

    @NotNull(message = "题型类别不能为空")
    @Column(name = "type_category", nullable = false, length = 20)
    private String typeCategory;

    @Size(max = 20, message = "父题型代码长度不能超过20个字符")
    @Column(name = "parent_type_code", length = 20)
    private String parentTypeCode;

    @Size(max = 1000, message = "描述信息长度不能超过1000个字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Size(max = 500, message = "答案格式要求长度不能超过500个字符")
    @Column(name = "answer_format", length = 500)
    private String answerFormat;

    @Column(name = "default_score")
    @Builder.Default
    private Integer defaultScore = 1;

    @Column(name = "max_score")
    private Integer maxScore;

    @Column(name = "min_score")
    @Builder.Default
    private Integer minScore = 1;

    @Size(max = 1000, message = "评分规则说明长度不能超过1000个字符")
    @Column(name = "scoring_rules", columnDefinition = "TEXT")
    private String scoringRules;

    @Column(name = "estimated_time") // 预估答题时间（秒）
    private Integer estimatedTime;

    @Column(name = "difficulty_level")
    private Integer difficultyLevel;

    @Column(name = "complexity_level")
    private Integer complexityLevel;

    @Column(name = "auto_gradeable")
    @Builder.Default
    private Boolean autoGradeable = true;

    @Column(name = "requires_manual_grading")
    @Builder.Default
    private Boolean requiresManualGrading = false;

    @Column(name = "supports_partial_credit")
    @Builder.Default
    private Boolean supportsPartialCredit = false;

    @Column(name = "supports_multiple_answers")
    @Builder.Default
    private Boolean supportsMultipleAnswers = false;

    @Column(name = "supports_random_options")
    @Builder.Default
    private Boolean supportsRandomOptions = false;

    @Column(name = "option_count")
    private Integer optionCount;

    @Size(max = 1000, message = "选项格式要求长度不能超过1000个字符")
    @Column(name = "option_format", columnDefinition = "TEXT")
    private String optionFormat;

    @Column(name = "min_option_count")
    private Integer minOptionCount;

    @Column(name = "max_option_count")
    private Integer maxOptionCount;

    @Column(name = "correct_option_count")
    private Integer correctOptionCount;

    @Size(max = 1000, message = "技术要求长度不能超过1000个字符")
    @Column(name = "technical_requirements", columnDefinition = "TEXT")
    private String technicalRequirements;

    @Size(max = 1000, message = "内容要求长度不能超过1000个字符")
    @Column(name = "content_requirements", columnDefinition = "TEXT")
    private String contentRequirements;

    @Size(max = 500, message = "适用场景长度不能超过500个字符")
    @Column(name = "suitable_scenarios", length = 500)
    private String suitableScenarios;

    @Column(name = "cognitive_levels_supported", length = 200)
    private String cognitiveLevelsSupported;

    @Column(name = "difficulty_levels_supported", length = 100)
    private String difficultyLevelsSupported;

    @Column(name = "color_code", length = 7)
    private String colorCode;

    @Column(name = "icon", length = 50)
    private String icon;

    @Column(name = "hierarchy_level")
    @Builder.Default
    private Integer hierarchyLevel = 1;

    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    @Column(name = "is_default")
    @Builder.Default
    private Boolean isDefault = false;

    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    @Column(name = "usage_count")
    @Builder.Default
    private Long usageCount = 0L;

    @Builder.Default
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private EntityStatus status = EntityStatus.ACTIVE;

    @Column(name = "version")
    @Builder.Default
    private Integer version = 1;

    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_type_code", referencedColumnName = "type_code",
               insertable = false, updatable = false)
    private QuestionType parentType;
}